//全局url http://ppak435ii5.52http.net
// var serverUrl = "http://localhost/contract/tp51/public/index.php/admin/";
var serverUrlAdmin = "https://desheng.hot68.cn/sxdx/api/public/index.php/admin/";
// var serverUrl = "https://desheng.hot68.cn/sxdx/api/public/index.php/app/";
var serverUrl = "http://localhost/contract/tp51/public/index.php/app/";
// 基础数据
var stateList='',knowledgeTypeList='',ownershipList='',conversionTypeList='',contractTypeList='',pricingStrategyList='',userList='', departmentList='',unitnatureList='',arealist='';

// 基础数据
layui.use(['layer','jquery','form','upload'], function() {
	var $ = layui.jquery;
	var layer = layui.layer;
	var form = layui.form;
	var upload = layui.upload;
    $.ajax({
        url:serverUrl+'Common/getListByType.html',
        success:function(data){
            // console.log(data);
            var dataArry=JSON.parse(data);
            console.log(dataArry);
            knowledgeTypeList=dataArry.obj.knowledge_type;
            ownershipList=dataArry.obj.ownership;
            conversionTypeList=dataArry.obj.conversion_type;
            contractTypeList=dataArry.obj.contract_type;
            arealist=dataArry.obj.area_list;
            stateList=dataArry.obj.state_list;
            unitnatureList=dataArry.obj.to_company_type;
        },
        error: function(err) {
            console.log(err);
        }
    });

    $.ajax({
        url:serverUrl+'Common/common_lst.html',
        success:function(data){
            // console.log(data);
            var dataArry=JSON.parse(data);
            userList=dataArry.obj.user_list;
            departmentList=dataArry.obj.department_list;
        },
        error: function(err) {
            console.log(err);
        }
    });

    window.getWhere=function (msg) {
		var $ = layui.jquery;
		var data = {};
		var t = $(msg).serializeArray();
		$.each(t, function() {
			data[this.name] = this.value;
		});
		return data;
    };
    window.showSearch=function (msg,param) {
        var storage = window.sessionStorage;
        var str = storage.getItem(param);
        if(str){
            var data = JSON.parse(str);
            form.val(msg, data);
        }
        form.render();
    };
    window.merge=function (res,names,indexs,str) {
        var data = res.data;
        var mergeIndex = 0;//定位需要添加合并属性的行数
        var mark = 1; //这里涉及到简单的运算，mark是计算每次需要合并的格子数
        var columsName = names;//需要合并的列名称
        var columsIndex = indexs;//需要合并的列索引值

        for (var k = 0; k < columsName.length; k++) { //这里循环所有要合并的列
            var trArr = $(".layui-table-body>.layui-table").find("tr");//所有行
            for (var i = 1; i < res.data.length; i++) { //这里循环表格当前的数据
                var tdCurArr = trArr.eq(i).find("td").eq(columsIndex[k]);//获取当前行的当前列
                var tdPreArr = trArr.eq(mergeIndex).find("td").eq(columsIndex[k]);//获取相同列的第一列

                if (data[i][str] === data[i-1][str] && (k === 0 || (k != 0 && data[i][columsName[k-1]] === data[i-1][columsName[k-1]]))) { //后一行的值与前一行的值做比较，相同就需要合并
                    mark += 1;
                    tdPreArr.each(function () {//相同列的第一列增加rowspan属性
                        $(this).attr("rowspan", mark);
                    });
                    tdCurArr.each(function () {//当前行隐藏
                        $(this).css("display", "none");
                    });
                }else {
                    mergeIndex = i;
                    mark = 1;//一旦前后两行的值不一样了，那么需要合并的格子数mark就需要重新计算
                }
            }
            mergeIndex = 0;
            mark = 1;
        }
    };

    window.getListByType = function getListByType(type,name='#business_id') {
        layer.load(2);
        $.post(serverUrl + 'Appuser/getBusinessListByType',{'type':type},function (res) {
            layer.closeAll('loading');
            if (res.success) {
                var infoStr='<option value="">全部</option>';
                for(var i in res.obj){
                    infoStr+='<option value="'+i+'">'+res.obj[i]+'</option>';
                }
                $(name).html(infoStr);
                form.render();
            }
            return false;
        },'json');
    }

    window.uploadImg = function uploadfile(dicName,imgShow='#pic',imgSrc='#img',imgName='#pic_url'){
        var src='';
        upload.render({
            elem: '#uploadfile' //绑定元素
            ,url: serverUrl + 'File/uploadImg' //上传接口
            ,data:{
                'dicName':dicName,
            }
            ,accept:'images'
            ,acceptMime:'image/*'
            ,done: function(res){
                //上传完毕回调
                if(res.success === true){
                    src = res.obj.file;
                    $(imgShow).show();
                    $(imgSrc).attr('src', src);
                    $(imgName).val(src);
                }else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    }

    window.uploadFile = function uploadfile(dicName){
        upload.render({
            elem: '#uploadfile' //绑定元素
            ,url: serverUrl + 'File/uploadFile' //上传接口
            ,data:{
                'dicName':dicName,
            }
            ,accept:'file'
            ,done: function(res){
                //上传完毕回调
                if(res.success === true){
                    var src = res.obj.file;
                    return src
                }else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    }
    window.toRegister = function toRegister(userMsg){
        $.post("https://desheng.hot68.cn/sxdx/api/public/index.php/app/Wxprogramehtml/getTwoTicket",
            {'corpid':userMsg['CorpId'],'urlAdress':window.location.href}, function(res) {
                let config = res.obj.config;
                let agentConfig =  res.obj.agentConfig;

                ww.register({
                    corpId: config.corpid,
                    agentId:config.agentid,
                    jsApiList:["selectEnterpriseContact","selectExternalContact","openUserProfile","selectPrivilegedContact"],
                    getConfigSignature() {
                        return ww.getSignature(config.ticket)
                    },
                    getAgentConfigSignature(){
                        return ww.getSignature(agentConfig.ticket)
                    },
                    onAgentConfigFail(){
                        console.log("签名失败");
                    },
                    onAgentConfigSuccess: function (res) {
                        regSunccFun();
                    },
                    error: function (res) {
                        //请求异常回调
                        console.log(res)
                        return false;
                    }
                })
            }, "json");
    }

});

function layerAlert(msg) {
	layui.use(['layer'], function() {
		var layer = layui.layer;
		layer.alert((msg), {
			title: '提示'
		});
	})
}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)")
    var r = window.location.search.substr(1).match(reg)
    if (r != null) return unescape(r[2])
    return null
}

function typeChange(obj) {
    for (var key in obj) {
        if (obj[key] == undefined) {
            obj[key] = '';
        }
    }
    return obj;
}

function regSunccFun(){
    ww.initOpenData({
        success: function (res) {
            console.log(WWOpenData, "初始化完成了哈哈哈哈")
            WWOpenData.bind(document.querySelector('ww-open-data'));
        },
        error: function (res) {
            //请求异常回调
            return false;
        }
    });
}